package com.cg.service;

import com.cg.service.OperateTokenService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName OperateTokenServiceImpl
 * @Description 获取操作的token实现类
 * @Author CG
 * @Date 2023/12/1 13:12
 **/
public class OperateTokenServiceImpl implements OperateTokenService {

    @Resource
    private RedisTemplate<String,String> redisTemplate;

    @Value("${operateTokenTimeOut}")
    private Long operateTokenTimeOut;
    @Override
    public String getOperateToken() {
        //生成token
        String operateToken = UUID.randomUUID().toString().replaceAll("-","");
        //保存到redis中
        redisTemplate.opsForValue().set(operateToken,operateToken,operateTokenTimeOut, TimeUnit.SECONDS);

        return operateToken;
    }

    @Override
    public Boolean check(String operateToken) {
        String value = redisTemplate.opsForValue().get(operateToken);
        if(value == null || value.equals("")){
            return false;
        }
        return true;
    }

    @Override
    public void deleteToken(String operateToken) {
        redisTemplate.delete(operateToken);
    }
}
